package com.kasax.skin.support.corel;

import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.view.ViewCompat;

import com.kasax.skin.support.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 皮肤属性
 */
public class SkinAttribute {
    // 支持换肤的属性
    private static final List<String> ATTRIBUTES = new ArrayList<>();
    // 皮肤包中的字体
    private  Typeface skinTypeface;

    // 缓存要换肤的view
    private List<SkinView> skinViews = new ArrayList<>();

    static {
        ATTRIBUTES.add("background");
        ATTRIBUTES.add("src");

        ATTRIBUTES.add("textColor");
        ATTRIBUTES.add("drawableLeft");
        ATTRIBUTES.add("drawableRight");
        ATTRIBUTES.add("drawableTop");
        ATTRIBUTES.add("drawableBottom");
    }

    public void setSkinTypeface(Typeface skinTypeface) {
        this.skinTypeface = skinTypeface;
    }

    public SkinAttribute(Typeface skinTypeFace) {
        this.skinTypeface = skinTypeFace;
    }

    public void load(View view, AttributeSet attrs) {
        LogUtils.i(getClass(), "load");
        List<SkinPain> skinPains = new ArrayList<>();
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            // 获取属性的名字
            String attributeName = attrs.getAttributeName(i);
            if (ATTRIBUTES.contains(attributeName)) {
                // 获取属性对应的值
                String attributeValue = attrs.getAttributeValue(i);
                if (attributeValue.startsWith("#")) {
                    // android:textColor="#ffffff" 像这种定死的色值肯定不是希望进行动态换肤的
                    continue;
                }
                int resId;
                // 判断前缀字符串，是否是"?"
                if (attributeValue.startsWith("?")) {
                    // 系统属性值，比如:android:textColor="?colorAccent,则属性ID的名称是从属性值下标1的位置开始，注意，它在R中只是一个Int类型的值
                    int attrId = Integer.parseInt(attributeValue.substring(1));
                    resId = SkinThemeUtils.getResId(view.getContext(), new int[]{attrId})[0];
                } else {
                    // android:textColor="@color/cardView_dark_background, 是这种场景
                    resId = Integer.parseInt(attributeValue.substring(1));
                }
                if (resId != 0) {
                    SkinPain skinPain = new SkinPain(attributeName, resId);
                    skinPains.add(skinPain);
                }
            }
        }
        if (!skinPains.isEmpty() || view instanceof TextView) {
            SkinView skinView = new SkinView(view, skinPains);
            skinView.applySkin(skinTypeface);
            skinViews.add(skinView);
        }
    }

    /**
     * 换肤
     */
    public void applySkin() {
        // 执行换肤
        for (SkinView skinView : skinViews) {
            skinView.applySkin(skinTypeface);
        }
    }

    static class SkinView {
        View view;
        List<SkinPain> skinPains;

        public SkinView(View view, List<SkinPain> skinPains) {
            this.view = view;
            this.skinPains = skinPains;
        }

        public void applySkin(Typeface skinTypeface) {
            applyTypeface(skinTypeface);
            for (SkinPain skinPain : skinPains) {
                int resId = skinPain.resId;
                Drawable left = null, top = null, right = null, bottom = null;
                switch (skinPain.attributeName) {
                    case "background":
                        Object background = SkinResources.getInstance().getBackground(resId);
                        if (background instanceof Integer) {
                            //Color
                            view.setBackgroundColor((Integer) background);
                        } else {
                            // Drawable
                            ViewCompat.setBackground(view, (Drawable) background);
                        }
                        break;
                    case "src":
                        background = SkinResources.getInstance().getBackground(resId);
                        if (background instanceof Integer) {
                            ((ImageView) view).setImageDrawable(new ColorDrawable((Integer) background));
                        } else {
                            // Drawable
                            ((ImageView) view).setImageDrawable((Drawable) background);
                        }
                        break;
                    case "textColor":
                        ((TextView) view).setTextColor(SkinResources.getInstance().getColorStateList(resId));
                        break;
                    case "drawableLeft":
                        left = SkinResources.getInstance().getDrawable(resId);
                        break;
                    case "drawableTop":
                        top = SkinResources.getInstance().getDrawable(resId);
                        break;
                    case "drawableRight":
                        right = SkinResources.getInstance().getDrawable(resId);
                        break;
                    case "drawableBottom":
                        bottom = SkinResources.getInstance().getDrawable(resId);
                        break;
                    default:
                        break;
                }
                if (null != left || null != right || null != top || null != bottom) {
                    ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(left, top, right,
                            bottom);
                }
            }

        }

        // 字体换肤
        private void applyTypeface(Typeface typeface) {
            if (view instanceof TextView) {
                ((TextView) view).setTypeface(typeface);
            }
        }
    }



    static class SkinPain {
        String attributeName;
        int resId;

        public SkinPain(String attributeName, int resId) {
            this.attributeName = attributeName;
            this.resId = resId;
        }


    }
}
